package com.notelapierdas.activities;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;

import com.notelapierdas.R;
import com.notelapierdas.graficobjects.DealListItem;
import com.notelapierdas.objects.Deal;
public class MainActivity extends ActionBarActivity implements OnItemClickListener,OnClickListener{
    /** Called when the activity is first created. */
    
	ArrayList<Deal> dealList;
	Button mainDealButton;
	
	@Override
    public void onCreate(Bundle savedInstanceState) {
		setContentView(R.layout.main_layout);
		super.onCreate(savedInstanceState);
    	
    	TextView mainDealTitleTextView = (TextView) findViewById(R.id.MainDealObject_TextView_title);
    	TextView mainDealOriginalPriceTextView = (TextView) findViewById(R.id.MainDealObject_TextView_originalprice);
    	TextView mainDealDealPriceTextView = (TextView) findViewById(R.id.MainDealObject_TextView_dealprice);
    	TextView mainDealDiscountTextView = (TextView) findViewById(R.id.MainDealObject_TextView_discount);
        
    	
        
    	ImageView mainDealImage = (ImageView) findViewById(R.id.MainDealObject_ImageView_picture);
    	
    	mainDealButton = (Button) findViewById(R.id.MainDealObject_Button_button);
        mainDealButton.setOnClickListener(this);
    	backImageButton.setVisibility(View.GONE);
                
        ////////////////////Get Data///////////////////////
        
        Bundle bundle = getIntent().getExtras();
        dealList = bundle.getParcelableArrayList(SplashActivity.STRING_CODE_DEAL_LIST);
        
        ///////////////////////////////////////////////////
       
        //////////////////////Main Deal////////////////////
        
        if(dealList!=null){
        	Deal mainDeal = dealList.get(0);
        	 //---------------------Creo Bitmap prueba---//
            Bitmap image = BitmapFactory.decodeResource(this.getResources(), R.drawable.image); //desp se saca
            mainDeal.setPicture(image); //despues lo saco
        	//mainDealImage.setImageBitmap(mainDeal.getPicture());  
            Bitmap resizedImage=android.graphics.Bitmap.createScaledBitmap(image, 180, 100,true);
            mainDealImage.setImageBitmap(resizedImage);
            
	        mainDealTitleTextView.setText(mainDeal.getTitle().getShortTitle());
	        mainDealDealPriceTextView.setText("$"+String.valueOf(mainDeal.getPrice().getDealPrice()));
	        mainDealOriginalPriceTextView.setText("$"+String.valueOf(mainDeal.getPrice().getOriginalPrice()));
	        mainDealDiscountTextView.setText(String.valueOf(mainDeal.getPrice().getDiscount())+"%");
        }        
        
        ///////////////////////////////////////////////////
        
        ///////////////////////List////////////////////////
        
        ListView lv= (ListView)findViewById(R.id.Main_ListView_listview);        
        String[] from = DealListItem.attList;
        int[] to = DealListItem.resList;
        List<HashMap<String, String>> fillMaps = new ArrayList<HashMap<String, String>>();

        for(int i=1;i<dealList.size();i++){
        	DealListItem item = new DealListItem(dealList.get(i));
        	HashMap<String, String> map = item.getMap();
            fillMaps.add(map);
       }       
        
        SimpleAdapter adapter = new SimpleAdapter(getApplicationContext(), fillMaps, R.layout.secondary_deal_object, from, to);
        lv.setAdapter(adapter);
        lv.setOnItemClickListener(this);

        //////////////////////////////////////////////////       
        
        
    }

	@Override
	public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
		
		if(dealList!=null&&dealList.size()>1){
			Intent intent = new Intent(this,DealActivity.class);
			intent.putExtra("com.notelapierdas.objects.Deal", dealList.get(arg2+1));
			startActivity(intent);
		}
		
		
		
	}

	@Override
	public void onClick(View v) {
		switch(v.getId()){
		case R.id.MainDealObject_Button_button:{
			if(dealList!=null){
				Intent intent = new Intent(this,DealActivity.class);
				intent.putExtra("com.notelapierdas.objects.Deal", dealList.get(0));
				startActivity(intent);
			}
			
			break;
		}
		}
		
	}
    
}